package com.coincalf.web;

import com.coincalf.dto.HzDTO;
import com.coincalf.entity.User;
import com.coincalf.framework.exception.GlobalDefaultException;
import com.coincalf.framework.http.Response;
import com.coincalf.service.HzRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 划转控制器
 * 资金从java这边交易所划转到php那边矿机或者从php那边矿机划转到java这边交易所
 */
@Slf4j
@RestController
@RequestMapping("hz")
public class HzController {

    @Autowired
    private HzRecordService hzRecordService;
    @Autowired
    private CommonController commonController;


    /**
     * 划转，矿机系统到交易所，PHP调用该接口增加用户余额，并将操作结果反馈给PHP
     *
     * @param hzDTO
     * @return
     */
    @PostMapping("/notice/hug/index")
    public Map<String, String> java2Php(@RequestBody HzDTO hzDTO) {
        //划转金额 币种名称
        Map<String, String> map = new HashMap<>(4);
        if (StringUtils.isEmpty(hzDTO.getFrom()) || StringUtils.isEmpty(hzDTO.getTo())
                || StringUtils.isEmpty(hzDTO.getName()) || StringUtils.isEmpty(hzDTO.getNum())
                || StringUtils.isEmpty(hzDTO.getPhone_code()) || StringUtils.isEmpty(hzDTO.getPhone())
                || StringUtils.isEmpty(hzDTO.getTrade_no())) {
            map.put("code", "100");
            map.put("message", "请求参数不能为空");
            return map;
        }
        if (!hzDTO.getFrom().equalsIgnoreCase("MINE") || !hzDTO.getTo().equals("EX")) {
            map.put("code", "101");
            map.put("message", "请求参数错误");
            return map;
        }
        if (!(hzDTO.getName().equals("AQS") || hzDTO.getName().equals("AQP") || hzDTO.getName().equals("USDT"))) {
            map.put("code", "101");
            map.put("message", "请求参数错误");
            return map;
        }
        BigDecimal num = new BigDecimal(hzDTO.getNum());
        if (num.compareTo(BigDecimal.ZERO) < 1) {
            map.put("code", "101");
            map.put("message", "请求参数错误");
            return map;
        }
        return hzRecordService.executeHzAndNotice(hzDTO);
    }


    /**
     * 划转 交易所到矿机系统
     *
     * @param request
     * @param hzDTO
     * @return
     */
    @PostMapping("exTx")
    public Response hz(HttpServletRequest request, @RequestBody HzDTO hzDTO) {
        User user = commonController.getUser(request);
        if (StringUtils.isEmpty(hzDTO.getNum()) || hzDTO.getCoinId() == null) {
            throw new GlobalDefaultException(-2);
        }
        if (new BigDecimal(hzDTO.getNum()).compareTo(BigDecimal.ZERO) < 1) {
            throw new GlobalDefaultException(-2);
        }
        return hzRecordService.executeHz(hzDTO, user);
    }

    @GetMapping("/list")
    public Response list(HttpServletRequest request,
                         @RequestParam(required = false) Integer pageNo,
                         @RequestParam(required = false) Integer pageSize,
                         @RequestParam(required = false) String name,
                         @RequestParam(required = false) Integer hzDirection) {
        String userMobile = commonController.getUserMobile(request);
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 10 : pageSize;
        return hzRecordService.list(pageNo, pageSize, name, hzDirection, userMobile);
    }

}
